<script lang="ts" setup>
import { ElMessage, FormInstance } from 'element-plus'
import { computed, onMounted, reactive, ref, defineAsyncComponent } from 'vue'
import { queryOrderAppeal, approveOrderAppeal } from '@/api/expense'
import { queryOrderAppealTableCol } from '@/api/expense/types/expense'
import { useTable } from '@/hooks/useTable'
import { getToken } from '@/utils/cache/cookies'

import { AppealStatus } from '@/types/enum'
import { useRouter } from 'vue-router'
defineOptions({ name: 'CyclingGrievances' })
const form = reactive({
  phone: '',
  feeType: '0',
  name: ''
})
const router = useRouter()
const formRef = ref<FormInstance | null>(null)
const { data, loading, pagination, load, handleCurrentChange, handleSizeChange, setCurrentPage } =
  useTable<queryOrderAppealTableCol>(queryOrderAppeal, getToken() ?? '', () => form)
const handleFormSearch = () => {
  setCurrentPage(1)

  load()
}
const reset = () => {
  formRef.value?.resetFields()
}
onMounted(() => {
  load()
})
const GrievanceApprovalRefundDialog = defineAsyncComponent(
  () => import('@/components/dialog/GrievanceApprovalRefund.vue')
)
const GrievanceApprovalRefundDialogRef = ref<InstanceType<
  typeof GrievanceApprovalRefundDialog
> | null>(null)
const approve = async (
  row: queryOrderAppealTableCol,
  status: '0' | '1',
  resolution: string = ''
) => {
  GrievanceApprovalRefundDialogRef.value?.setData(
    row.amount,
    row.orderAppealId,
    status,
    row.orderId
  )
  GrievanceApprovalRefundDialogRef.value?.open()
}
const formatStatus = computed(() => {
  return (status: AppealStatus) => {
    switch (status) {
      case 'PENDING':
        return '待审核'
      case 'APPROVED':
        return '已通过'
      case 'REJECTED':
        return '未通过'
      default:
        break
    }
  }
})
const formatStatusStyle = computed(() => {
  return (status: AppealStatus) => {
    switch (status) {
      case 'PENDING':
        return 'color:var(--xy-warning-color)'
      case 'APPROVED':
        return 'color:var(--xy-success-color)'
      case 'REJECTED':
        return 'color:var(--xy-danger-color)'
      default:
        break
    }
  }
})
const RefuseApplicationDialog = defineAsyncComponent(
  () => import('@/components/dialog/RefuseApplication.vue')
)
const RefuseApplicationDialogRef = ref<InstanceType<typeof RefuseApplicationDialog> | null>(null)

const openDialog = (row: queryOrderAppealTableCol) => {
  RefuseApplicationDialogRef.value?.setData(row.orderAppealId, '0')
  RefuseApplicationDialogRef.value?.open()
}
const seeLocus = (row: queryOrderAppealTableCol) => {
  router.push({
    name: 'TimeBikeOrderLocus',
    query: {
      bikeId: row.bikeId,
      orderId: row.orderId
    }
  })
}
const toUserInfo = (row: queryOrderAppealTableCol) => {
  router.push({
    name: 'UserInfo',
    query: {
      userId: row.userId,
      phone: row.phone,
      selectType: 'charge'
    }
  })
}
</script>

<template>
  <div class="xy-container">
    <el-form :model="form" ref="formRef" inline>
      <el-form-item prop="name" label="申诉人">
        <el-input v-model="form.name" placeholder="请输入申诉人" />
      </el-form-item>
      <el-form-item prop="phone" label="手机号">
        <el-input v-model="form.phone" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item prop="feeType" label="申诉类型">
        <el-select v-model="form.feeType" placeholder="请选择申诉类型" style="width: 140px">
          <el-option label="不限" value="0" />
          <el-option label="行程收费" value="1" />
          <el-option label="违规收费" value="2" />
          <el-option label="其他收费" value="3" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleFormSearch">查询</el-button>
        <el-button plain @click="reset">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="data" v-loading="loading">
      <el-table-column prop="bikeCode" label="车辆编号" min-width="100px" align="center" />
      <el-table-column prop="name" label="申诉人姓名" min-width="100px" align="center" />
      <el-table-column prop="phone" label="手机号" min-width="150px" align="center">
        <template #default="scope">
          <span cursor-pointer @click="toUserInfo(scope.row)" class="color-primary">
            {{ scope.row.phone }}</span
          >
        </template>
      </el-table-column>
      <el-table-column prop="orderId" label="行程编号" min-width="200px" align="center" />
      <el-table-column prop="storeName" label="所属门店" align="center" min-width="150px" />
      <el-table-column
        prop="appealType"
        label="申诉类型"
        min-width="100px"
        align="center"
        show-overflow-tooltip
      />
      <el-table-column prop="amount" label="申请金额" align="center" />
      <el-table-column label="申诉图片" align="center">
        <template #default="scope">
          <el-image
            v-for="(img, index) in scope.row?.imageUrls.split(',')"
            :key="index"
            :src="img"
            :preview-src-list="scope.row?.imageUrls.split(',')"
            w-50
            h-50
            :preview-teleported="true"
          />
        </template>
      </el-table-column>
      <el-table-column
        prop="appealReason"
        label="申诉原因"
        min-width="140px"
        align="center"
        show-overflow-tooltip
      />
      <el-table-column prop="status" label="申诉状态" align="center">
        <template #default="scope">
          <span :style="formatStatusStyle(scope.row.status)">{{
            formatStatus(scope.row.status)
          }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="appealDate" label="申诉时间" min-width="200px" align="center" />
      <el-table-column prop="resolution" label="处理备注" min-width="200px" align="center" />
      <el-table-column prop="processedTime" label="处理时间" min-width="200px" align="center" />
      <el-table-column label="行程轨迹" align="center" fixed="right">
        <template #default="scope">
          <el-button link class="is-show" @click="seeLocus(scope.row)" v-if="scope.row.orderId"
            >查看</el-button
          >
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" fixed="right" min-width="200px">
        <template #default="scope">
          <el-button
            link
            class="is-show"
            @click="approve(scope.row, '1')"
            v-if="scope.row.status === 'PENDING'"
            >通过</el-button
          >
          <el-button
            link
            class="is-show"
            @click="openDialog(scope.row)"
            v-if="scope.row.status === 'PENDING'"
            >不通过</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div flex justify-end mt-20>
      <el-pagination
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"
        v-model:current-page="pagination.pageNum"
        v-model:page-size="pagination.pageSize"
        background
        :total="pagination.total"
        layout="total,prev,pager,next,jumper,sizes"
      />
    </div>
    <refuse-application-dialog ref="RefuseApplicationDialogRef" @refresh="load" />
    <grievance-approval-refund-dialog ref="GrievanceApprovalRefundDialogRef" />
  </div>
</template>

<style scoped></style>
